home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / source / src / plane / _convex_hull.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  1.4 KB  |  85 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  _convex_hull.c
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #include <LEDA/convex_hull.h>
  16. #include <LEDA/plane.h>
  17.  
  18. list<point> CONVEX_HULL(list<point> L)
  19.   if (L.length() < 3) return L;
  20.  
  21.   list<point> CH;
  22.   list_item last;
  23.   point p;
  24.  
  25.   L.sort();  // sort points lexicographically
  26.  
  27.  
  28.   // initialize convex hull with first two points
  29.  
  30.   p = L.pop();
  31.   CH.append(p);
  32.   while (L.head() == p) L.pop();
  33.   last = CH.append(L.pop());
  34.  
  35.  
  36.   // scan remaining points
  37.  
  38.   forall(p,L)
  39.   {
  40.     if (p == CH[last]) continue;  // multiple point
  41.  
  42.     // compute upper tangent (p,up)
  43.  
  44.     list_item up = last;
  45.     list_item it = CH.cyclic_succ(up);
  46.  
  47.     while (left_turn(CH[it],CH[up],p))
  48.     { up = it;
  49.       it = CH.cyclic_succ(up);
  50.      }
  51.  
  52.  
  53.     // compute lower tangent (p,low)
  54.  
  55.     list_item low = last;
  56.     it = CH.cyclic_pred(low);
  57.  
  58.     while (right_turn(CH[it],CH[low],p))
  59.     { low = it;
  60.       it = CH.cyclic_pred(low);
  61.      }
  62.  
  63.  
  64.     // remove all points between up and low
  65.  
  66.     if (up != low)
  67.     { it = CH.cyclic_succ(low);
  68.  
  69.       while (it != up)
  70.       { CH.del(it);
  71.         it = CH.cyclic_succ(low);
  72.        }
  73.      }
  74.  
  75.     // insert new point
  76.  
  77.     last = CH.insert(p,low);
  78.  
  79.    }
  80.  
  81.   return CH;
  82. }
  83.